package com.itheima.reggie.controlle;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.BindingType;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    //修改的时候删除缓存中的套餐商品数据以防 数据不显示
    //@CacheEvict(cacheNames = "setmealList", key = "#setmealDto.categoryId+':'+#setmealDto.status")
    @CacheEvict(cacheNames = "setmealList", allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveAll(setmealDto);
        return R.success("");
    }

    /**
     * 分页条件查询
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(@RequestParam("page") Integer page,
                                    @RequestParam("pageSize") Integer pageSize,
                                    String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.pageMathSetmeal(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 根据id批量删除
     */
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 启用/禁用
     * 参数int状态 和 ids
     */
    @PostMapping("/status/{status}")
    public R<String> yesAanNo(@PathVariable Integer status,
                              Long[] ids) {
        //设置状态值 根据id设置
        setmealService.update()
                .set("status", status)
                .in("id", ids).update();
        return R.success("修改成功");
    }

    /**
     * 根据id回显
     */
    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id) {
        //调用service层
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改
     * 参数使用dto接收
     */
    @PutMapping
    //修改的时候删除缓存中的套餐商品数据以防 数据空值
    //删除单个
    // @CacheEvict(cacheNames = "setmealList", key = "#setmealDto.categoryId+':'+#setmealDto.status")
    //删除全部
    @CacheEvict(cacheNames = "setmealList", allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithSetmeaDto(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 查询套餐商品
     *
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    //多个查询存入缓存
    @Cacheable(cacheNames = "setmealList", key = "#categoryId + ':' + #status", unless = "#result.data.size() == 0")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        List<Setmeal> setmealList = setmealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, categoryId)
                .eq(status != null, Setmeal::getStatus, status).list();
        return R.success(setmealList);
    }

    /**
     * 查询套餐菜品
     *
     * @param
     * @return
     */
    @GetMapping("/dish/{id}")
    //单个查询存入缓存  不要和多个查的key一样
    @Cacheable(cacheNames = "setmeal", key = "#id")
    public R<List<DishDto>> dishList(@PathVariable Long id) {
        List<DishDto> dishDtoList = setmealService.dishList(id);
        return R.success(dishDtoList);
    }
}
